package com.sailorj.medical.web.servlet;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @program: medical_project
 * @description: 抽象基类
 * @author: huangshengjun
 * @create: 2020-06-07 12:48
 **/
@WebServlet(name = "BaseServlet")
public class BaseServlet extends HttpServlet {

    private static final long serialVersionUID = 8196938314965941620L;
    private static final Logger LOG = LogManager.getLogger(BaseServlet.class);
    protected void process(HttpServletRequest request,
                           HttpServletResponse response, String path) throws ServletException,
            IOException {

        RequestDispatcher rd = request.getRequestDispatcher(path);
        rd.forward(request, response);

    }
    protected void error(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException,
            IOException {
        RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
        rd.forward(request, response);

    }

    public String write(Object word, HttpServletRequest request, HttpServletResponse response) {
        try {
            // String wordJSON = JSON.toJSONString(word);
            PrintWriter out = response.getWriter();
            out.println(word);
            out.flush();
            out.close();
        } catch (Exception e) {
            LOG.info("write出错：" + e);
        }
        return null;
    }
}
